package com.itbour.web.action.yjx.user;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.yjx.user.YjxUserDto;
import com.itbour.service.yjx.user.YjxUserInfoService;
import com.opensymphony.xwork2.ActionContext;


/**
 * 	管理一键秀 用户信息
 * 	@author 李琦
 */
public class ManageYjxUserInfo extends BaseAction {
	
	/**
	 */
	private static final long serialVersionUID = 1L;
	private YjxUserInfoService yjxUserInfoService;// 一键秀userService
	private String findMobile;
	private String findName;
	private String findUserId;
	private String findType;
	private String findDtS;// 开始时间
	private String findDtE;// 结束时间
	private Map<String, Object> mapResult=new HashMap<String, Object>();
	
	
	/*	分页获取一键秀用户信息
	 * 	参数 page/页码 rows/行数
	 * 	可选参数：用户ID 用户类型 用户名称 用户手机号
	 * 	分页显示 一页显示10条数据
	 */
	public String pageUserInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findMobile)){
				parm.put("mobile", findMobile);
			}
			if(StringUtils.isNotEmpty(findName)){
				parm.put("nickname", findName);
			}
			if (StringUtils.isNotEmpty(findUserId)) {
				parm.put("user_id", findUserId);
			}
			if(StringUtils.isNotBlank(findType)){
				parm.put("user_type",findType);
			}
			if (StringUtils.isNotBlank(findDtS)){
				parm.put("start_time",findDtS);
			}
			if (StringUtils.isNotBlank(findDtE)){
				parm.put("end_time",findDtE);
			}
			Page page=yjxUserInfoService.getYjxUserInfo(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	
	// 导出 一键秀用户信息
	public void exportYjxUserInfo() throws IOException{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			Map<String,Object> parm=new HashMap<String, Object>();
			if (StringUtils.isNotBlank(findDtS)){
				parm.put("start_time",findDtS);
			}
			if (StringUtils.isNotBlank(findDtE)){
				parm.put("end_time",findDtE);
			}
			parm.put("pageSize", 1000);
			parm.put("skipResults", 0);
			List<YjxUserDto> liUser = yjxUserInfoService.getUserInfo(parm);
			
	 		StringBuilder sb = new StringBuilder();
			String SEP = ",";
			Date date = new Date();
			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sim2 = new SimpleDateFormat("yyyyMMddHHmmss");
			String date1 = sim.format(date);
			String date2 = sim2.format(date);
		    
			sb.append("一键秀__用户信息").append(SEP).append(date1).append("\n");
			sb.append("用户头像")
			.append(SEP).append("用户ID")
			.append(SEP).append("用户昵称")
			.append(SEP).append("用户手机号")
			.append(SEP).append("用户类型")
			.append(SEP).append("会员状态")
			.append(SEP).append("创建时间")
			.append(SEP).append("修改时间")
			.append("\n");
			
			YjxUserDto yu = null;
			for (int i = 0; i < liUser.size(); i++) {
				yu = liUser.get(i);
				String ty = "类型未知";
				switch(yu.getUser_type()){
				case 0:ty = "后台用户";break;
				case 1:ty = "普通用户";break;
				case 2:ty = "soft用户";break;
				}
				String vipSt = "非会员";
				if (yu.getMember_id()!=null){
					vipSt = "会员用户";
				}
				sb=sb.append("\""+Null2Str(yu.getProfile_photo())+"\"")
						.append(SEP).append("\""+Null2Str(yu.getUser_id())+"\"")
						.append(SEP).append("\""+Null2Str(yu.getNickname())+"\"")
						.append(SEP).append("\""+Null2Str(yu.getMobile())+"\"")
						.append(SEP).append("\""+ty+"\"")
						.append(SEP).append("\""+vipSt+"\"")
						.append(SEP).append("\""+yu.getCreate_time()+"\"")
						.append(SEP).append("\""+yu.getUpdate_time()+"\"");
				sb.append("\n");
			}
			
			String fileName = "Yjx_UserInfo_" + date2 + ".csv";
	        String output = sb.toString();
	        HttpServletResponse response = ServletActionContext.getResponse();
	        response.addHeader("Content-Disposition", "attachment; filename="+fileName); 
	        response.setContentType("application/octet-stream"); 
	        response.setCharacterEncoding("UTF8"); 
	        response.getWriter().write(output); 
	        response.flushBuffer();
			
		}
	}
	
	private String Null2Str(Object in){
		return (in==null)?"":String.valueOf(in);
	}
	
	/* 	获取一个 一键秀用户信息
	 * 	参数：单个用户ID
	 */
	private List<YjxUserDto> listUser;
	public String getOneYjxUserInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null&&user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer user_id=Integer.parseInt(request.getParameter("id"));
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("user_id",user_id);
			listUser=yjxUserInfoService.getOneYjxUserInfo(parm);
		}
		return SUCCESS;
	}
	
	/*
	 * 修改 一键秀用户信息
	 * 参数：用户账号/account
	 * 参数：用户昵称/nickname
	 * 参数：用户类型/user_type
	 */
	private YjxUserDto userInfo;
	public void updUserInfo() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null&&user.getUser_id()!=null){
			Map<String, Object> parm =new HashMap<String, Object>();
			if(userInfo.getUser_id()!=null){
				if (userInfo.getAccount() != null){
					parm.put("account",userInfo.getAccount());
				}
				if (userInfo.getNickname() != null){
					parm.put("nickname",userInfo.getNickname());
				}
				if (userInfo.getUser_type() != null){
					parm.put("user_type",userInfo.getUser_type());
				}
				if (userInfo.getEvaluation() != null){
					parm.put("evaluation",userInfo.getEvaluation());
				}
				parm.put("user_id",userInfo.getUser_id());
				parm.put("update_by",user.getUser_id());
				boolean b  = yjxUserInfoService.updYjxUserInfo(parm);
				if (b){
					this.writeJSON("ok");
				} else{
					this.writeJSON("no");
				}
			}
		} else {
			this.writeJSON("no");
		}
	}	
	
	/*	分页获取一键秀第三方账号信息
	 * 	可选参数：用户ID/用户类型/用户名称/用户手机号
	 */
	public String pageGetThirdUserInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> parm=new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findMobile)){
				parm.put("mobile", findMobile);
			}
			if (StringUtils.isNotEmpty(findName)){
				parm.put("nickname", findName);
			}
			if (StringUtils.isNotEmpty(findUserId)) {
				parm.put("user_id", findUserId);
			}
			if (StringUtils.isNotBlank(findType)){
				parm.put("user_type",findType);
			}
			Page page=yjxUserInfoService.getYjxThirdUserInfo(parm, pageNo, pageSize);
			if (mapResult!=null){
				mapResult.clear();
			}
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	
	// 以下生成 set和get方法
	
	public YjxUserInfoService getYjxUserInfoService() {
		return yjxUserInfoService;
	}
	public String getFindDtS() {
		return findDtS;
	}
	public void setFindDtS(String findDtS) {
		this.findDtS = findDtS;
	}
	public String getFindDtE() {
		return findDtE;
	}
	public void setFindDtE(String findDtE) {
		this.findDtE = findDtE;
	}
	public String getFindUserId() {
		return findUserId;
	}
	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}

	public void setYjxUserInfoService(YjxUserInfoService yjxUserInfoService) {
		this.yjxUserInfoService = yjxUserInfoService;
	}
	public String getFindMobile() {
		return findMobile;
	}
	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}
	public String getFindName() {
		return findName;
	}
	public void setFindName(String findName) {
		this.findName = findName;
	}
	public String getFindType() {
		return findType;
	}
	public void setFindType(String findType) {
		this.findType = findType;
	}
	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public List<YjxUserDto> getListUser() {
		return listUser;
	}
	public void setListUser(List<YjxUserDto> listUser) {
		this.listUser = listUser;
	}
	public YjxUserDto getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(YjxUserDto userInfo) {
		this.userInfo = userInfo;
	}
	
}
